package com.webDemo02.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.security.auth.message.callback.SecretKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import com.mysql.fabric.Response;
import com.webDemo02.Service.EmpService;
import com.wenDemo02.entity.Emp;


@WebServlet(urlPatterns="/emp.do")
public class EmpServlet  extends BaseServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	EmpService  empService = new EmpService();
	public void  queryEmps(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
	
		List<Emp> emps = empService.queryEmps();
		request.setAttribute("emps", emps);
		request.getRequestDispatcher("emps.jsp").forward(request, response);
	}
	
	public void  addEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		JSONObject json =new JSONObject();

	   String emp_name = request.getParameter("emp_name");
		double  emp_sal =Double.parseDouble(request.getParameter("emp_sal"));
	
		Emp emp= new Emp();
		emp.setEmp_name(emp_name);
		emp.setEmp_sal(emp_sal);
		System.out.println(emp);
		boolean boo = empService.addEmp(emp);
		if(boo){
			json.put("msg", "1");
		}
		else{
			json.put("msg", "0");
		}
		PrintWriter writer = response.getWriter();
		writer.print(json);
	}
  
	public void  deleteEmp(HttpServletRequest request, HttpServletResponse response) throws IOException{
		JSONObject json =new JSONObject();
		int emp_id  = Integer.parseInt(request.getParameter("data_id"));
		boolean  bo = empService.delete(emp_id);
		if(bo){
			json.put("m", "1");
		}else{
			json.put("m", "0");
		}
		PrintWriter writer = response.getWriter();
		writer.print(json);
	}
	
	  public void  updateEmp(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		JSONObject json =new JSONObject();
		   int emp_id  = Integer.parseInt(request.getParameter("emp_id"));
		   String emp_name = request.getParameter("emp_name");
			double  emp_sal =Double.parseDouble(request.getParameter("emp_sal"));
		
			Emp emp= new Emp();
			System.out.println(emp_id);
			emp.setEmp_id(emp_id);
			emp.setEmp_name(emp_name);
			emp.setEmp_sal(emp_sal);
            System.out.println(emp);
		boolean  bo = empService.updateEmp(emp);
		if(bo){
			json.put("msg", "1");
			request.setAttribute("emp", emp);
			System.out.println(emp);
		}else{
			json.put("msg", "0");
		}
		PrintWriter writer = response.getWriter();
		writer.print(json);
	}
	  
	  
		public void  queryById(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException{
		
			int emp_id  = Integer.parseInt(request.getParameter("id"));
			Emp emp = empService.queryById(emp_id);
			if(emp!=null){
				request.setAttribute("emp",emp);
				System.out.println(emp);
				request.getRequestDispatcher("updateEmp.jsp").forward(request, response);
			}
		}
		
		
		
		public void deleteEmps(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException{
			String[] ids=request.getParameterValues("ids[]");
			for(int id=0;id<ids.length;id++){
				empService.delete(id);
			}
			
            PrintWriter out = response.getWriter();
			
			out.flush();
			out.close();
		}
}
